package com.springvn.sample.controller;

import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.springvn.sample.persistence.Employee;
import com.springvn.sample.services.IEmployeeService;
import com.springvn.sample.utils.Consts;

@Controller
public class EmployeeController {

	@Autowired
	private IEmployeeService employeeService;

	private static final Logger logger = LoggerFactory
			.getLogger(HomeController.class);

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "employee", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		
		logger.info("home controller");
		model.addAttribute("employee", new Employee());
		model.addAttribute("employeeList", employeeService.getAllEmployees());

		return Consts.EMPLOYEE_INDEX;
	}

	@RequestMapping(value = "employee/add", method = RequestMethod.GET)
	public String addEmployeeGET(@ModelAttribute(value = "employee") Employee employee,
			BindingResult result){
		
		return "/employee/add";
	}
	
	@RequestMapping(value = "employee/add", method = RequestMethod.POST)
	public String addEmployeePOST(@ModelAttribute(value = "employee") Employee employee,
			BindingResult result){
		
		employeeService.addEmployee(employee);
		
		return "/employee/add";
	}

}